package com.songtai.action.admin.news;

import java.io.PrintWriter;
import java.util.Date;

import com.songtai.action.SongtaiActionSupport;
import com.songtai.po.News;
import com.songtai.service.NewsService;
import com.songtai.util.CommenUtil;
import com.songtai.util.Constants;
import com.songtai.util.Pager;

/**
 * @author 赵中杨
 * @email zhaozhongyang717@gmail.com
 * @date 2010 2010-4-19 下午04:23:08
 */
public class NewsAction extends SongtaiActionSupport {

	/**
	 * 序列号
	 */
	private static final long serialVersionUID = 7054768678960137063L;
	private static final String CONTENT_TYPE = "text/html; charset=utf-8";
	// 标题
	private String title;
	// 内容
	private String content;
	// 状态
	private String status;
	// 节点ID
	private Long nodeid;
	// 新闻列表
	private Pager<News> newsItem;
	// 当前页
	private int currentPage;
	// 新闻ID
	private String newsid;
	// 摘要
	private String summary;

	// 方法
	private String method;
	// 新闻
	private News newsData;
	private NewsService newsService;

	/**
	 * 添加新闻
	 * 
	 * @return
	 * @throws Exception
	 */
	public String addNews() throws Exception {
		if (!Constants.STATUS_ONLINE.equals(status)) {
			status = Constants.STATUS_OFFLINE;
		}
		News news = new News();
		news.setContent(content);
		news.setNid(nodeid);
		news.setRecordTime(new Date());
		news.setStatus(status);
		news.setTitle(title);
		news.setSummary(summary);
		newsService.addNews(news);
		method = SUCCESS;
		addActionMessage(getText("add.success"));
		return SUCCESS;
	}

	/**
	 * 获取节点下的所有新闻
	 * 
	 * @return
	 * @throws Exception
	 */
	public String getNews() throws Exception {
		if (nodeid < 1) {
			return null;
		}
		if (currentPage < 1) {
			currentPage = 1;
		}
		newsItem = newsService.getNewsListByNodeid(nodeid, currentPage, Constants.DEFAULT_PAGESIEZE);
		return SUCCESS;
	}

	/**
	 * 删除新闻
	 * 
	 * @return
	 * @throws Exception
	 */
	public String deleteNews() throws Exception {
		response.setContentType(CONTENT_TYPE);
		PrintWriter out = response.getWriter();
		if (CommenUtil.isEmpty(newsid)) {
			out.print("fail");
		} else {
			boolean flag = newsService.deleteNews(newsid);
			if (flag) {
				out.print("success");
			} else {
				out.print("fail");
			}
		}
		return null;
	}

	/**
	 * 恢复新闻
	 * 
	 * @return
	 * @throws Exception
	 */
	public String resumeNews() throws Exception {
		response.setContentType(CONTENT_TYPE);
		PrintWriter out = response.getWriter();
		if (CommenUtil.isEmpty(newsid)) {
			out.print("fail");
		} else {
			int flag = newsService.changeNewsStatus(Constants.STATUS_ONLINE, Constants.STATUS_OFFLINE, newsid);
			if (flag > 0) {
				out.print("success");
			} else {
				out.print("fail");
			}
		}
		return null;
	}

	/**
	 * 暂停新闻
	 * 
	 * @return
	 * @throws Exception
	 */
	public String pauseNews() throws Exception {
		response.setContentType(CONTENT_TYPE);
		PrintWriter out = response.getWriter();
		if (CommenUtil.isEmpty(newsid)) {
			out.print("fail");
		} else {
			int flag = newsService.changeNewsStatus(Constants.STATUS_OFFLINE, Constants.STATUS_ONLINE, newsid);
			if (flag > 0) {
				out.print("success");
			} else {
				out.print("fail");
			}
		}
		return null;
	}

	/**
	 * 按ID获取新闻
	 * 
	 * @return
	 * @throws Exception
	 */
	public String getNewsById() throws Exception {
		if (CommenUtil.isEmpty(newsid)) {
			return null;
		}
		newsData = newsService.getNewsById(Long.parseLong(newsid));
		if (CommenUtil.isEmpty(method)) {
			return LOGIN;
		}
		return SUCCESS;
	}

	public String editNews() throws Exception {
		if (!Constants.STATUS_ONLINE.equals(status)) {
			status = Constants.STATUS_OFFLINE;
		}
		newsService.updateNews(newsData);
		method = SUCCESS;
		addActionMessage(getText("edit.success"));
		return SUCCESS;
	}

	/**
	 * @return the title
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * @param title
	 *            the title to set
	 */
	public void setTitle(String title) {
		this.title = title;
	}

	/**
	 * @return the content
	 */
	public String getContent() {
		return content;
	}

	/**
	 * @param content
	 *            the content to set
	 */
	public void setContent(String content) {
		this.content = content;
	}

	/**
	 * @return the status
	 */
	public String getStatus() {
		return status;
	}

	/**
	 * @param status
	 *            the status to set
	 */
	public void setStatus(String status) {
		this.status = status;
	}

	/**
	 * @return the nodeid
	 */
	public Long getNodeid() {
		return nodeid;
	}

	/**
	 * @param nodeid
	 *            the nodeid to set
	 */
	public void setNodeid(Long nodeid) {
		this.nodeid = nodeid;
	}

	/**
	 * @return the newsService
	 */
	public NewsService getNewsService() {
		return newsService;
	}

	/**
	 * @param newsService
	 *            the newsService to set
	 */
	public void setNewsService(NewsService newsService) {
		this.newsService = newsService;
	}

	/**
	 * @return the newsItem
	 */
	public Pager<News> getNewsItem() {
		return newsItem;
	}

	/**
	 * @param newsItem
	 *            the newsItem to set
	 */
	public void setNewsItem(Pager<News> newsItem) {
		this.newsItem = newsItem;
	}

	/**
	 * @return the currentPage
	 */
	public int getCurrentPage() {
		return currentPage;
	}

	/**
	 * @param currentPage
	 *            the currentPage to set
	 */
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	

	/**
	 * @return the newsid
	 */
	public String getNewsid() {
		return newsid;
	}

	/**
	 * @param newsid the newsid to set
	 */
	public void setNewsid(String newsid) {
		this.newsid = newsid;
	}

	/**
	 * @return the method
	 */
	public String getMethod() {
		return method;
	}

	/**
	 * @param method
	 *            the method to set
	 */
	public void setMethod(String method) {
		this.method = method;
	}

	/**
	 * @return the newsData
	 */
	public News getNewsData() {
		return newsData;
	}

	/**
	 * @param newsData
	 *            the newsData to set
	 */
	public void setNewsData(News newsData) {
		this.newsData = newsData;
	}

	/**
	 * @return the summary
	 */
	public String getSummary() {
		return summary;
	}

	/**
	 * @param summary
	 *            the summary to set
	 */
	public void setSummary(String summary) {
		this.summary = summary;
	}

}
